package com.card.admin.servelt;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.card.user.User;
import com.card.user.UserCurrency.CurrencyKind;
import com.card.user.UserUtils;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class QueryServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/plain;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		String action =req.getParameter("action");
		if("userquery".equals(action)) {
			String userId = req.getParameter("uid");
			JSONObject jsonObj = new JSONObject();
			User user = UserUtils.getUserByPrimaryKey(userId);
			List<User> userList = Lists.newArrayList();
			if(user != null) userList.add(user);
			JSONArray rows = new JSONArray();
			int total = userList.size()/50;
			jsonObj.put("page", 1);
			jsonObj.put("total", userList.size()%50==0?total:total+1); // 总页数
			jsonObj.put("records", userList.size()); // 总记录数
			for (User u : userList) {
				JSONObject cell =new JSONObject();
				cell.put("ID", u.getPrimaryKey());
				cell.put("NickName", u.userExtra().getNickName());
				cell.put("Gold", u.userCurrency().get(CurrencyKind.Diamonds));
				rows.put(cell);
			}
			jsonObj.put("rows", rows);
			res.getWriter().print(jsonObj);
		}
	}
	
}
